home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dskut / mindos11.zip / MREAD.C < prev    next >
C/C++ Source or Header  |  1991-03-22  |  5KB  |  193 lines

  1. /*  mread.c     -- read a file from a Minix file system */
  2. /*  Copyright 1988,1991 Steven W. Harrold - All rights reserved. */
  3. /*  $Header: MREAD.C_V 1.5 91/03/22 07:53:37 SWH Exp $ */
  4.  
  5. #include    <stdio.h>
  6. #include    <ctype.h>
  7. #include    <stdlib.h>
  8. #include    <string.h>
  9. #include    "mfs.h"
  10. #include    "dev.h"
  11.  
  12. DEVINIT
  13. READ_BLOCK
  14. GET_INODE
  15. MINIX_TO_DOS
  16.  
  17.  
  18. /*==================================================================*/
  19. main (argc, argv)
  20. int     argc ;
  21. char    *argv[] ;
  22. {
  23.     int     drive ;
  24.     char    drid = 'A' ;
  25.     char    *dp = DRIVES ;
  26.  
  27.     byte    buf[BLOCK_SIZE] ;
  28.     int     blkno ;
  29.     struct super_block  sblk ;
  30.  
  31.     struct inode    *iarea, *ip ;
  32.     int     inode_blkno ;
  33.     int     inode_blkct ;
  34.     int     inode_count ;
  35.  
  36.     struct directory    *darea ;
  37.     struct devdata      *ddata ;
  38.  
  39.     int     i, j, k, n ;
  40.     char    *path[MAX_PATH/2+1] ;
  41.     BOOL    text = FALSE ;
  42.     FILE    *outfd ;
  43.     char    *tp ;
  44.     char    minixfn[MAX_PATH+NAME_SIZE+2] ;
  45.  
  46.  
  47.     printf ("**** Copies a file from a Minix system to MS-DOS ****\n") ;
  48.     printf ("\n") ;
  49.  
  50. /*  Does he want help?
  51.  */
  52.     if ((argc >= 2) && (argv[1][0] == '-') && (argv[1][1] == '?'))
  53.     {
  54. printf ("Copyright 1988,1991 Steven W. Harrold - All rights reserved\n") ;
  55.         printf ("Version %s\n", VERSION) ;
  56.         printf ("Usage:   %s  [-a] [drid:]minixfn dosfn\n", argv[0]) ;
  57.         printf ("'-a' translates LF to CRLF, binary otherwise\n") ;
  58.         printf ("'drid' is a letter from the set [a-z], default: 'a'\n") ;
  59.         printf ("'minixfn' is a fully qualified Minix filename\n") ;
  60.         printf ("'dosfn' is standard DOS filename\n") ;
  61.         exit (0) ;
  62.     }
  63.  
  64. /*  Get the option flag
  65.  */
  66.     if ((argc >= 2) && (argv[1][0] == '-') && (argv[1][1] == 'a'))
  67.     {
  68.         text = TRUE ;
  69.         argc-- ;
  70.         argv++ ;
  71.     }
  72.     else
  73.         text = FALSE ;
  74.  
  75. /*  Fetch the Minix drive identifier
  76.  */
  77.     if (argc != 3)
  78.     {
  79.         fprintf (stderr, "Must supply both Minix and DOS filenames\n") ;
  80.         exit (2) ;
  81.     }
  82.  
  83.     if (argv[1][1] == ':')
  84.     {
  85.         drid = toupper(argv[1][0]) ;
  86.         if ((drid < 'A') || (drid > 'Z'))
  87.             drid = 'A' ;
  88.         argv[1] += 2 ;
  89.     }
  90.     drive = strchr (dp, drid) - dp ;
  91.     ddata = devinit (drive, 0) ;
  92.     if (!ddata)
  93.     {
  94.         printf ("Cannot initialize device 0x%02X, Dstatus=%d\n",
  95.                 drive, Dstatus) ;
  96.         exit (2) ;
  97.     }
  98.  
  99. /*  Break Minix filename into path components
  100.  */
  101.     if (argv[1][0] != '/')
  102.     {
  103.         fprintf (stderr,
  104.                 "You must supply a fully qualified Minix filename\n") ;
  105.         exit (2) ;
  106.     }
  107.     strcpy (minixfn, argv[1]) ;
  108.  
  109.     tp = strtok (&argv[1][1], "/") ;
  110.     i = 0 ;
  111.     while (tp)
  112.     {
  113.         path[i++] = tp ;
  114.         tp = strtok (NULL, "/") ;
  115.     }
  116.     path[i] = NULL ;
  117.  
  118. /*  The super block
  119.  */
  120.     blkno = SUPER_BLOCK ;
  121.     read_block (buf, blkno, ddata) ;
  122.     memcpy (&sblk, buf, sizeof(sblk)) ;
  123.  
  124.     if ((sblk.s_magic != SUPER_MAGIC)   ||
  125.         (sblk.s_ninodes < 1)            ||
  126.         (sblk.s_nzones < 1)             ||
  127.         (sblk.s_imap_blocks < 1)        ||
  128.         (sblk.s_zmap_blocks < 1)        )
  129.     {
  130.         fprintf (stderr, "+++++++++++++++++++++++++++++++++++++++\n") ;
  131.         fprintf (stderr, "+++ THIS IS NOT A PROPER MINIX DISK +++\n") ;
  132.         fprintf (stderr, "+++++++++++++++++++++++++++++++++++++++\n") ;
  133.         exit (2) ;
  134.     }
  135.  
  136. /*  The i-node blocks
  137.  */
  138.     inode_blkno = SUPER_BLOCK + sblk.s_imap_blocks +
  139.                                 sblk.s_zmap_blocks + 1 ;
  140.     inode_blkct = sblk.s_ninodes * sizeof(struct inode) + (BLOCK_SIZE-1) ;
  141.     inode_blkct /= BLOCK_SIZE ;
  142.     inode_count = inode_blkct * sizeof(struct inode) ;
  143.  
  144.     iarea = calloc (inode_count+1, sizeof(struct inode)) ;
  145.     if (!iarea)
  146.         exit(3) ;
  147.  
  148.     ip = iarea + 1 ;
  149.     blkno = inode_blkno ;
  150.     j = BLOCK_SIZE / sizeof(struct inode) ;
  151.  
  152.     for (i=1; i<=inode_blkct; i++, ip += j, blkno++)
  153.         read_block (ip, blkno, ddata) ;
  154.  
  155. /*  Now, the inode search through the directories
  156.  */
  157.     ip = iarea + ROOT_INODE ;                   /* start with root */
  158.     ip = get_inode(ddata, iarea, ip, path) ;    /* recursively scan */
  159.  
  160.     if (!ip)
  161.     {
  162.         fprintf (stderr, "Cannot locate Minix file '%s'\n", minixfn) ;
  163.         exit (1) ;
  164.     }
  165.  
  166.     if (!(ip->i_mode & I_REGULAR))
  167.     {
  168.         fprintf (stderr, "Minix file '%s' is not a regular file\n",
  169.                          minixfn) ;
  170.         exit(1) ;
  171.     }
  172.  
  173. /*  Finally, copy the file to DOS-land
  174.  */
  175.     if ((outfd=fopen(argv[2], "wb")) == NULL)
  176.     {
  177.         fprintf (stderr, "Cannot open DOS file '%s' due to\n", argv[2]) ;
  178.         perror (NULL) ;
  179.         exit (1) ;
  180.     }
  181.  
  182.     minix_to_dos(text, ip, ddata, outfd) ;
  183.     fclose (outfd) ;
  184.  
  185. /*  Clean up
  186.  */
  187.     free (iarea) ;
  188.  
  189. } /* main() */
  190.  
  191.  
  192. /*---eof---*/
  193.